package com.tic.robot.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuyou.framework.common.base.BaseActivity;
import com.tic.robot.R;

/**
 * 带有返回按钮、标题和右边自定义view的通用标题栏Activity
 * Created by chuangjiewang on 2016/7/25.
 */
public class BaseTitleActivity extends BaseActivity {

    private RelativeLayout mTitleLayout;

    private TextView mTitle;

    private ImageView mBackIV;

    private LinearLayout mRootLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_title);

        setToolBar();
    }

    private void setToolBar() {

        mRootLayout = (LinearLayout) findViewById(R.id.activity_root);
        setContent();

        mBackIV = (ImageView) findViewById(R.id.title_iv_back);
        mBackIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        mTitleLayout = (RelativeLayout) findViewById(R.id.title_layout);

        mTitle = (TextView) findViewById(R.id.title_name);
    }

    /**
     * 设置布局
     */
    protected void setContent() {
        if (getContentLayout() > 0) {
            mRootLayout.addView(LayoutInflater.from(this).inflate(getContentLayout(), mRootLayout, false));
        }
    }

    protected int getContentLayout() {
        return -1;
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    /**
     * 添加右边自定义view
     *
     * @param view
     */
    public void addRightCustomView(View view) {
        if (view != null && mTitleLayout != null) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.rightMargin = (int) (5 * getResources().getDisplayMetrics().density);
            view.setLayoutParams(params);
            mTitleLayout.addView(view);
        }
    }
}
